/*
 * CreateEditRepaymentDialog.java
 * 
 * Created: Aug 7, 2015
 * 
 * Copyright (c) ExpertPB 2015
 * All information contained herein is, and remains the property of
 * ExpertPB and its suppliers, if any.
 */
package com.emex.app.stuff.supplier;

import com.emex.app.shared.data.Repayment;
import com.emex.app.shared.data.enums.PayMethod;
import com.emex.app.stuff.utils.DateTimeService;
import com.emex.app.stuff.utils.UIUtils;
import javax.swing.DefaultComboBoxModel;

/**
 *
 * @author Sergey
 */
public class CreateEditRepaymentDialog extends javax.swing.JDialog {
    
    private RepaymentTableModel repaymentTableModel;
    private Repayment repayment;

    /**
     * Creates new form CreateEditRepaymentDialog
     */
    public CreateEditRepaymentDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        datePicker.setDate(DateTimeService.getInstance().getDate());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        summaField = new javax.swing.JTextField();
        summaDownField = new javax.swing.JTextField();
        clientFIOField = new javax.swing.JTextField();
        positionNameField = new javax.swing.JTextField();
        datePicker = new org.jdesktop.swingx.JXDatePicker();
        saveButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(org.openide.util.NbBundle.getMessage(CreateEditRepaymentDialog.class, "CreateEditRepaymentDialog.title")); // NOI18N
        setResizable(false);

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(CreateEditRepaymentDialog.class, "CreateEditRepaymentDialog.jLabel1.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(CreateEditRepaymentDialog.class, "CreateEditRepaymentDialog.jLabel2.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(CreateEditRepaymentDialog.class, "CreateEditRepaymentDialog.jLabel3.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getMessage(CreateEditRepaymentDialog.class, "CreateEditRepaymentDialog.jLabel4.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel5, org.openide.util.NbBundle.getMessage(CreateEditRepaymentDialog.class, "CreateEditRepaymentDialog.jLabel5.text")); // NOI18N

        summaField.setText(org.openide.util.NbBundle.getMessage(CreateEditRepaymentDialog.class, "CreateEditRepaymentDialog.summaField.text")); // NOI18N

        summaDownField.setText(org.openide.util.NbBundle.getMessage(CreateEditRepaymentDialog.class, "CreateEditRepaymentDialog.summaDownField.text")); // NOI18N

        clientFIOField.setText(org.openide.util.NbBundle.getMessage(CreateEditRepaymentDialog.class, "CreateEditRepaymentDialog.clientFIOField.text")); // NOI18N

        positionNameField.setText(org.openide.util.NbBundle.getMessage(CreateEditRepaymentDialog.class, "CreateEditRepaymentDialog.positionNameField.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(saveButton, org.openide.util.NbBundle.getMessage(CreateEditRepaymentDialog.class, "CreateEditRepaymentDialog.saveButton.text")); // NOI18N
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(cancelButton, org.openide.util.NbBundle.getMessage(CreateEditRepaymentDialog.class, "CreateEditRepaymentDialog.cancelButton.text")); // NOI18N
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 212, Short.MAX_VALUE)
                        .addComponent(saveButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cancelButton))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(13, 13, 13)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(summaField, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(summaDownField, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(clientFIOField, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(positionNameField)
                            .addComponent(datePicker, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(datePicker, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(summaField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(summaDownField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(clientFIOField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(positionNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(saveButton)
                    .addComponent(cancelButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        if (null == datePicker.getDate() || summaField.getText().isEmpty()
                    || summaDownField.getText().isEmpty()
                    || clientFIOField.getText().isEmpty()
                    || positionNameField.getText().isEmpty()) {
            UIUtils.showErrorMessage("Все поля обязательны для заполнения");
            return;
        }
        
        boolean isCreate = false;
        if (null == repayment) {
            repayment = new Repayment();
            isCreate = true;
        }
        
        repayment.setDate(datePicker.getDate());
        repayment.setSumma(Double.valueOf(summaField.getText().replaceAll(",", ".")));
        repayment.setSummDown(Double.valueOf(summaDownField.getText().replaceAll(",", ".")));
        repayment.setPositionName(positionNameField.getText());
        repayment.setFio(clientFIOField.getText());
        if (isCreate) {
            repaymentTableModel.dataAdded(repayment);
        } else {
            repaymentTableModel.dataUpdated();
        }
        setVisible(false);
    }//GEN-LAST:event_saveButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        setVisible(false);
    }//GEN-LAST:event_cancelButtonActionPerformed

    public RepaymentTableModel getRepaymentTableModel() {
        return repaymentTableModel;
    }

    public void setRepaymentTableModel(RepaymentTableModel repaymentTableModel) {
        this.repaymentTableModel = repaymentTableModel;
    }

    public Repayment getRepayment() {
        return repayment;
    }

    public void setRepayment(Repayment repayment) {
        this.repayment = repayment;
        if (null != repayment) {
            datePicker.setDate(repayment.getDate());
            summaField.setText(repayment.getSumma().toString());
            summaDownField.setText(repayment.getSummDown().toString());
            positionNameField.setText(repayment.getPositionName());
            clientFIOField.setText(repayment.getFio());
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JTextField clientFIOField;
    private org.jdesktop.swingx.JXDatePicker datePicker;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField positionNameField;
    private javax.swing.JButton saveButton;
    private javax.swing.JTextField summaDownField;
    private javax.swing.JTextField summaField;
    // End of variables declaration//GEN-END:variables
}
